<?php


namespace app\data\controller\api;


use think\admin\Controller;
use think\facade\Db;

class Article extends Auth
{

    /**
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 文章列表
     */
    public function list(){
        $name = !empty(input('name')) ? input('name') : '';
        $where = [
            'a.status'        => 1,
            'a.deleted'       => 0,
//            'a.examine'       => 2
//            'a.type'          => 2
        ];
        $list = Db::name('data_news_item')
            ->alias('a')
            ->join('data_member b',"a.publisher = b.id",'left')
            ->field('a.*,b.username,b.headimg')
            ->order('sort desc,id desc')
//            ->whereLike('name',$name)
            ->where($where)
            ->where('name','like','%' . $name . '%')
            ->paginate(sysconf('listrow'))
            ->toArray();
        foreach ($list['data'] as $key => $val){
            $val['like'] = $this->app->db->name('data_member_like')->where(['a_id' => $val['id'],'user_id' => $this->mid])->value('id');
            if (!empty($val['like'])){
                $list['data'][$key]['m_like'] = 1;
            }else{
                $list['data'][$key]['m_like'] = 2;
            }
            if (!empty($val['images'])){
                $list['data'][$key]['images'] = json_decode($val['images']);
            }
            if (empty($val['publisher'])){
                $list['data'][$key]['username'] = "匿名用户";
                $list['data'][$key]['headimg'] = 'https://api.hnsyc.com/safefile/后台管理员.jpg';
            }
        }
        if (!empty($list)){
            $this->success('获取成功',$list);
        }else{
            $this->error('获取失败');
        }
    }

    /**
     * 内外仓文章列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cang_list(){
        //内外仓 type  分类 cate_id
        $data = input();
        $where = [
            'cate_id'           => $data['cate_id'],
            'a.status'          => 1,
            'a.deleted'         => 0,
            'a.examine'         => 2
        ];
        $name = !empty(input('name')) ? input('name') : '';
        if ($data['type'] == 1){
            $where['publisher'] = $this->mid;
        }else{
            $where['type'] = $data['type'];
        }
        $list = $this->app->db->name('data_news_item')
            ->alias('a')
            ->join('data_member b',"a.publisher = b.id",'left')
            ->field('a.*,b.username,b.headimg')
            ->order('sort desc,id desc')
            ->where($where)
            ->where('name','like','%' . $name . '%')
            ->paginate(sysconf('listrow'))
            ->toArray();
        foreach ($list['data'] as $key => $val){
            $val['like'] = $this->app->db->name('data_member_like')->where(['a_id' => $val['id'],'user_id' => $this->mid])->value('id');
            if (!empty($val['like'])){
                $list['data'][$key]['m_like'] = 1;
            }else{
                $list['data'][$key]['m_like'] = 2;
            }
            if (!empty($val['images'])){
                $list['data'][$key]['images'] = json_decode($val['images']);
            }
        }
        if ($list){
            $this->success('获取成功',$list);
        }else{
            $this->error('获取失败');
        }
    }


    /**
     * 获取文章评论
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException

    public function comment(){
        $data = $this->_vali([
            'cid.require'       => '未找到该文章信息'
        ]);
        $comment = $this->app->db->name('data_news_x_comment')->where(['cid' => $data['cid']])->paginate(sysconf('listrow'));
        if (!empty($comment)){
            $this->success('获取成功',$comment);
        }else{
            $this->error('暂无评论');
        }
    }*/

    /**
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * 点赞功能
     */
    public function like(){
        /*$data = $this->_vali([
            'a_id.require'      => '未查找到该文章信息',
            'token.require'     => '未登录'
        ]);*/
        $data = input();
        unset($data['token']);

        $article = $this->app->db->name('data_news_item')->where('id='.$data['a_id'])->find();
        if (empty($article) /*|| $article['deleted'] == 1*/){
            $this->error('文章不存在');
        }

        if ($article['status'] == 0){
            $this->error('文章已被禁用');
        }

        $map = [
            'user_id'       => $this->mid,
            'a_id'          => $data['a_id'],
        ];
        $like = Db::name('data_member_like')->where($map)->find();
        if ($like){
            $this->error('您已经点过赞了');
        }
        $insert = Db::name('data_member_like')->insert($map);
        $num_like = Db::name('data_member_like')->where('a_id='.$data['a_id'])->count();
        $result = Db::name('data_news_item')->where('id='. $data['a_id'])->save(['num_like' => $num_like]);
        $arr = [
            'user_id'       => $this->mid,
            'cid'           => $data['a_id'],
            'content'       => '赞了你的作品'
        ];
        $publisher = Db::name('data_news_item')->where('id='.$data['a_id'])->value('publisher');
        if ($publisher !== $this->mid){
            Db::name('data_user_message')->insert($arr);
        }
        if ($result == 1 && $insert){
            $this->success('点赞成功');
        }else{
            $this->error('点赞失败');
        }
    }

    /**
     * 文章分类列表
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function cateList(){
        $list = $this->app->db->name('data_news_cate')->where('deleted = 0')->select();
        if (!empty($list)){
            $this->success('获取成功',$list);
        }else{
            $this->error('获取失败');
        }
    }

}